'use strict'
const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name =  'platform' // page title

const port = process.env.port || process.env.npm_config_port || 9527 // dev port

// CDN资源
// const assetsCDN = {
//   externals: {
//     vue: 'Vue',
//     'vue-router': 'VueRouter',
//     vuex: 'Vuex',
//     'element-ui': 'ELEMENT',
//     axios: 'axios'
//   },
//   css: [
//     '//unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css'
//   ],
//   js: [
//     '//unpkg.com/vue@2.6.10/dist/vue.min.js',
//     '//unpkg.com/vue-router@3.0.2/dist/vue-router.min.js',
//     '//unpkg.com/vuex@3.1.0/dist/vuex.min.js',
//     '//unpkg.com/element-ui@2.13.0/lib/index.js',
//     '//unpkg.com/axios@0.18.1/dist/axios.min.js'
//   ]
// }

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/': {
        target: 'http://localhost:8081/',
        changeOrigin: true,
        // 代理携带cookie
        cookieDomainRewrite: {
          '*': 'localhost'
        }
      }
    }
    // before: require('./mock/mock-server.js')
  },
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
    // 生产环境使用CDN
    // externals: process.env.NODE_ENV === 'production'
    //   ? assetsCDN['externals']
    //   : {}
  },
  chainWebpack(config) {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    // set CDN to inject index.html
    // config
    //   .when(process.env.NODE_ENV === 'production',
    //     config => {
    //       config
    //         .plugin('html')
    //         .tap(options => {
    //           options[0].cdn = assetsCDN
    //           return options
    //         })
    //     }
    //   )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          // config
          //   .plugin('ScriptExtHtmlWebpackPlugin')
          //   .after('html')
          //   .use('script-ext-html-webpack-plugin', [{
          //   // `runtime` must same as runtimeChunk name. default is `runtime`
          //     inline: /runtime\..*\.js$/
          //   }])
          //   .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
